package sisi.invoices;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.zkoss.zk.ui.Executions;
import org.zkoss.zk.ui.Sessions;
import org.zkoss.zk.ui.SuspendNotAllowedException;
import org.zkoss.zk.ui.event.CreateEvent;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.event.ForwardEvent;
import org.zkoss.zk.ui.sys.ComponentsCtrl;
import org.zkoss.zk.ui.util.ConventionWires;
import org.zkoss.zul.Datebox;
import org.zkoss.zul.Decimalbox;
import org.zkoss.zul.Intbox;
import org.zkoss.zul.ListModelList;
import org.zkoss.zul.Listbox;
import org.zkoss.zul.Checkbox;
import org.zkoss.zul.Listcell;
import org.zkoss.zul.Listitem;
import org.zkoss.zul.ListitemRenderer;
import org.zkoss.zul.Messagebox;
import org.zkoss.zul.Textbox;
import org.zkoss.zul.Toolbarbutton;
import org.zkoss.zul.Window;

import javax.persistence.EntityManager;

import sisi.General;

@SuppressWarnings({ "rawtypes", "unchecked", "serial" })
public class CustinvoicedetailWindow extends Window implements 
				org.zkoss.zk.ui.ext.AfterCompose {
//window
private Window windowCustinvoicedetail, windowEditCustinvoicedetail;
private Textbox tbCerca;
private Listbox boxCustinvoicedetail;
private List lista_Custinvoicedetail;
private boolean lNuovo;
private Toolbarbutton buttonSeleziona;
private ListModelList listModelList;
private Integer idSelezionato = 0;

protected Map<String, Object> args;
private Object CustinvoicedetailItem;
private Custinvoicedetail modCustinvoicedetail;
// edit
private Textbox type;
private Textbox note;
private Intbox id;
private Intbox recnum;
private Textbox curr;
private Intbox custid;
private Textbox custname;
private Intbox custnuminvoice;
private Datebox dtup;
private |TIPO DATO NON TROVATO| exchangerate;
private Textbox nfysnote;
private Intbox order;
private |TIPO DATO NON TROVATO| paidamount;
private Datebox paydt;
private Textbox stat;
private Textbox vatarticle;
private Checkbox billitem;
private |TIPO DATO NON TROVATO| custtaxed;
private |TIPO DATO NON TROVATO| custtotsalevat;
private |TIPO DATO NON TROVATO| custvat;
private |TIPO DATO NON TROVATO| custvatperc;
private Checkbox paid;
private Textbox partno;
private Intbox productid;
private Textbox productname;
private |TIPO DATO NON TROVATO| quantity;
private Intbox seq;
private |TIPO DATO NON TROVATO| totalamount;
private Textbox um;
private |TIPO DATO NON TROVATO| unitprice;
private Textbox vatcode;

		public void onCreate$windowCustinvoicedetail(Event event) {

		CreateEvent ce = (CreateEvent) ((ForwardEvent) event).getOrigin();
		args = (Map<String, Object>) ce.getArg();
		String tastoSeleziona = (String) args.get("tastoSeleziona");
		tastoSeleziona = (tastoSeleziona == null || tastoSeleziona.isEmpty() ? "N": tastoSeleziona);
		boolean lTastoSeleziona = tastoSeleziona.equals("S");
		if (!lTastoSeleziona) {
			buttonSeleziona.setVisible(false);
		}
		idSelezionato = (Integer) args.get("idselezionato");

		listModelList = new ListModelList();
		lista_Custinvoicedetail = new CustinvoicedetailController().getList(null);
		listModelList.addAll(lista_Custinvoicedetail);
		boxCustinvoicedetail.setModel(listModelList);
		boxCustinvoicedetail.setItemRenderer(new CustinvoicedetailItemRenderer());

		if (idSelezionato!=null && idSelezionato!=0){
		for (int i = 0; i < lista_Custinvoicedetail.size(); i++) {
			Custinvoicedetail record = (Custinvoicedetail)lista_Custinvoicedetail.get(i);
			if (record.getId()==idSelezionato.intValue()){
				boxCustinvoicedetail.setSelectedIndex(i);
				break;
			}
		}
	}

}

	public void afterCompose() {

		// wire variables
		ConventionWires.wireVariables(this, this);

		// auto forward
		ConventionWires.addForwards(this, this);

	}

	public void onOK$tbCerca(Event event){
		cerca();
	}

	public void onClick$buttonCerca(Event event) {
		cerca();
	}


	private void cerca(){
		listModelList = new ListModelList();
		lista_Custinvoicedetail = new CustinvoicedetailController().getList(tbCerca.getValue());
		listModelList.addAll(lista_Custinvoicedetail);
		boxCustinvoicedetail.setModel(listModelList);
		boxCustinvoicedetail.setItemRenderer(new CustinvoicedetailItemRenderer());
	}

	public void onDoubleClicked(Event event) {
		if (buttonSeleziona.isVisible()) {
			seleziona();
		} else {
			modifica(false);
		}
	}

	public void onClick$buttonSeleziona(Event event) {

		seleziona();

}

	public void seleziona() {
		if (boxCustinvoicedetail.getSelectedIndex() >= 0 && buttonSeleziona.isVisible()) {
			Object oCustinvoicedetail = boxCustinvoicedetail.getSelectedItem().getAttribute("rigaCustinvoicedetail");
			String codiceSelezionato = ""+((Custinvoicedetail) oCustinvoicedetail).getId();
			Sessions.getCurrent().setAttribute("codiceCustinvoicedetailSelezionato", codiceSelezionato);
			windowCustinvoicedetail.detach();
		}
	}

	public void onClick$buttonNuovo(Event event) {
		modifica(true);
	}

	public void onClick$buttonModifica(Event event) {
		modifica(false);
	}

	private void modifica(boolean nuovo){
		try {

			int nIndex = boxCustinvoicedetail.getSelectedIndex();

			if (!nuovo && nIndex == -1) { // nessuna riga selezionata

				Messagebox.show("Select a row to modify", "Information", Messagebox.OK, Messagebox.INFORMATION);

			} else {
				HashMap map = new HashMap();
				if (nuovo){
					map.put("lNuovo", true);
				}
				map.put("boxCustinvoicedetail", boxCustinvoicedetail);
				Window finestraEditCustinvoicedetail = (Window) Executions.createComponents("/editCustinvoicedetail.zul", null, map);
				finestraEditCustinvoicedetail.doModal();
			}

		} catch (SuspendNotAllowedException e) {
			e.printStackTrace();
		}
	}

	public void onClick$buttonCancella(Event event) {
	// TODO Auto-generated method stub

	}

	public class CustinvoicedetailItemRenderer implements ListitemRenderer {

		public void render(Listitem li, Object data, int index) {
			new Listcell(""+((Custinvoicedetail) data).getRecnum()).setParent(li);
			new Listcell(""+((Custinvoicedetail) data).getProductname()).setParent(li);
			li.setAttribute("rigaCustinvoicedetail", data);
			ComponentsCtrl.applyForward(li, "onDoubleClick=onDoubleClicked");
		}
	}


// EDIT: NEW/MODIFY
	public void onCreate$windowEditCustinvoicedetail(Event event) {

		CreateEvent ce = (CreateEvent) ((ForwardEvent) event).getOrigin();
		args = (Map<String, Object>) ce.getArg();

		boxCustinvoicedetail = (Listbox) args.get("boxCustinvoicedetail");

		if (args.containsKey("lNuovo")) {
			lNuovo = true;
		} else {
			lNuovo = false;
		}

		if (!lNuovo) {
			CustinvoicedetailItem = boxCustinvoicedetail.getSelectedItem().getAttribute("rigaCustinvoicedetail");
			modCustinvoicedetail = new CustinvoicedetailController().refresh((Custinvoicedetail) CustinvoicedetailItem);
			CustinvoicedetailItem = modCustinvoicedetail;
			windowEditCustinvoicedetail.setTitle("Edit Custinvoicedetail");
			//categoryname.setFocus(true);
			//id.setValue(modCategorie.getCategoryid());
			//categoryname.setValue(modCategorie.getCategoryname());
			//description.setValue(modCategorie.getDescription());
			type.setValue(modCustinvoicedetail.getType());
			note.setValue(modCustinvoicedetail.getNote());
			id.setValue(modCustinvoicedetail.getId());
			recnum.setValue(modCustinvoicedetail.getRecnum());
			curr.setValue(modCustinvoicedetail.getCurr());
			custid.setValue(modCustinvoicedetail.getCustid());
			custname.setValue(modCustinvoicedetail.getCustname());
			custnuminvoice.setValue(modCustinvoicedetail.getCustnuminvoice());
			dtup.setValue( (java.util.Date) (modCustinvoicedetail.getDtUp()));
			exchangerate.setValue(modCustinvoicedetail.getExchangerate());
			nfysnote.setValue(modCustinvoicedetail.getNfysNote());
			order.setValue(modCustinvoicedetail.getOrder());
			paidamount.setValue(modCustinvoicedetail.getPaidamount());
			paydt.setValue( (java.util.Date) (modCustinvoicedetail.getPaydt()));
			stat.setValue(modCustinvoicedetail.getStat());
			vatarticle.setValue(modCustinvoicedetail.getVatarticle());
			billitem.setChecked(modCustinvoicedetail.getBillitem());
			custtaxed.setValue(modCustinvoicedetail.getCusttaxed());
			custtotsalevat.setValue(modCustinvoicedetail.getCusttotsalevat());
			custvat.setValue(modCustinvoicedetail.getCustvat());
			custvatperc.setValue(modCustinvoicedetail.getCustvatperc());
			paid.setChecked(modCustinvoicedetail.getPaid());
			partno.setValue(modCustinvoicedetail.getPartno());
			productid.setValue(modCustinvoicedetail.getProductid());
			productname.setValue(modCustinvoicedetail.getProductname());
			quantity.setValue(modCustinvoicedetail.getQuantity());
			seq.setValue(modCustinvoicedetail.getSeq());
			totalamount.setValue(modCustinvoicedetail.getTotalamount());
			um.setValue(modCustinvoicedetail.getUm());
			unitprice.setValue(modCustinvoicedetail.getUnitprice());
			vatcode.setValue(modCustinvoicedetail.getVatcode());
		} else {
			windowEditCustinvoicedetail.setTitle("New Custinvoicedetail");
			//categoryname.setFocus(true);
		}

	}

	public void onClick$btnAnnulla(Event event){
		windowEditCustinvoicedetail.detach();
	}

	public void onClick$btnConferma(Event event){
		if (lNuovo){
			modCustinvoicedetail = new Custinvoicedetail();
		} else {

		}
		String testoMsg = "";

		modCustinvoicedetail.setType(type.getValue());
		modCustinvoicedetail.setNote(note.getValue());
		modCustinvoicedetail.setId(id.getValue());
		modCustinvoicedetail.setRecnum(recnum.getValue());
		modCustinvoicedetail.setCurr(curr.getValue());
		modCustinvoicedetail.setCustid(custid.getValue());
		modCustinvoicedetail.setCustname(custname.getValue());
		modCustinvoicedetail.setCustnuminvoice(custnuminvoice.getValue());
		modCustinvoicedetail.setDtUp( (dtup.getValue() != null ? (java.sql.Date) sisi.General.date2Sql(dtup.getValue()): null) );
		modCustinvoicedetail.setExchangerate(exchangerate.getValue());
		modCustinvoicedetail.setNfysNote(nfysnote.getValue());
		modCustinvoicedetail.setOrder(order.getValue());
		modCustinvoicedetail.setPaidamount(paidamount.getValue());
		modCustinvoicedetail.setPaydt( (paydt.getValue() != null ? (java.sql.Date) sisi.General.date2Sql(paydt.getValue()): null) );
		modCustinvoicedetail.setStat(stat.getValue());
		modCustinvoicedetail.setVatarticle(vatarticle.getValue());
		modCustinvoicedetail.setBillitem(billitem.isChecked());
		modCustinvoicedetail.setCusttaxed(custtaxed.getValue());
		modCustinvoicedetail.setCusttotsalevat(custtotsalevat.getValue());
		modCustinvoicedetail.setCustvat(custvat.getValue());
		modCustinvoicedetail.setCustvatperc(custvatperc.getValue());
		modCustinvoicedetail.setPaid(paid.isChecked());
		modCustinvoicedetail.setPartno(partno.getValue());
		modCustinvoicedetail.setProductid(productid.getValue());
		modCustinvoicedetail.setProductname(productname.getValue());
		modCustinvoicedetail.setQuantity(quantity.getValue());
		modCustinvoicedetail.setSeq(seq.getValue());
		modCustinvoicedetail.setTotalamount(totalamount.getValue());
		modCustinvoicedetail.setUm(um.getValue());
		modCustinvoicedetail.setUnitprice(unitprice.getValue());
		modCustinvoicedetail.setVatcode(vatcode.getValue());

		if (lNuovo){
			new CustinvoicedetailController().add(modCustinvoicedetail);
			testoMsg = "New Custinvoicedetail: "+ General.trimNull( modCustinvoicedetail.getProductname() );
		} else {
			new CustinvoicedetailController().update(modCustinvoicedetail);
			testoMsg = "Updating Custinvoicedetail: "+ General.trimNull( modCustinvoicedetail.getProductname() );
		}

		General.showNotify( testoMsg, windowEditCustinvoicedetail);

		windowEditCustinvoicedetail.detach();

		// aggiorna listbox

		if (lNuovo){
			ListModelList lml = (ListModelList) boxCustinvoicedetail.getListModel();
			lml.add(modCustinvoicedetail);
			boxCustinvoicedetail.setSelectedIndex(lml.indexOf(modCustinvoicedetail));
		} else {
			ListModelList lml = (ListModelList) boxCustinvoicedetail.getListModel();
			Object oCustinvoicedetailItem = boxCustinvoicedetail.getSelectedItem().getAttribute("rigaCustinvoicedetail");
			if (lml.indexOf(oCustinvoicedetailItem) == -1) {
				// non trovato... quindi non lo aggiorno
			} else {
				lml.set(lml.indexOf(oCustinvoicedetailItem), modCustinvoicedetail);
			}
		}
	}

}
//
// fine
